package com.machine.service.order.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.machine.bean.Componentpart;
import com.machine.bean.OrderForm;
import com.machine.bean.PageBean;
import com.machine.dao.order.OrderDao;
import com.machine.service.order.OrderServer;

@Component(value = "orderServer")
public class OrderServerImpl implements OrderServer {

	private OrderDao orderDao;

	public OrderDao getOrderDao() {
		return orderDao;
	}

	@Resource
	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	@Override
	public PageBean findOrder(String providerName,String buyName,int page, int length) {
		String hql= "from OrderForm orderForm where orderForm.buyName = '" + buyName+"' OR orderForm.providerName='"+providerName+"'";
		int allRow = orderDao.getAllRowCount(hql); // 总记录数
		int totalPage = PageBean.countTotalPage(length, allRow); // 总页数
		int offset = PageBean.countOffset(length, page); // 当前页开始记录
		//int length = length; // 每页记录数
		int currentPage = PageBean.countCurrentPage(page);
		List<Componentpart> list = orderDao.queryForPage(providerName, buyName,offset,length); // "一页"的记录
		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(length);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		System.out.println("allrow : " + allRow);
		System.out.println("totalPage : " + totalPage);
		System.out.println("offset : " + offset);
		System.out.println("currentPage : " + currentPage);
		return pageBean;
		//return orderDao.findOrder( providerName, buyName,offset,length);
	}

	@Override
	public int addOrder(OrderForm orderForm) {
		return orderDao.addOrder(orderForm);
	}

	@Override
	public int delOrder(OrderForm orderForm) {

		return orderDao.delOrder(orderForm);
	}

	@Override
	public int updateOrder(OrderForm orderForm) {
		return orderDao.updateOrder(orderForm);
	}

	@Override
	public int deleteByIdList(List<Integer> list) {
		return orderDao.deleteByIdList(list);
	}

	@Override
	public OrderForm findById(Integer id) {
		return orderDao.findById(id);
	}


	

}
